
#include "ControlDef.h"

//已删除全部注释

//CButton
//////////////////////////////////////////////////////////////////////////
CButton::CButton(WCHAR* Name,int X,int Y,int Width,int Height,WCHAR* Text,BOOL FlatStyle)
{
	Type=CT_Button;
	this->Name=wstrdup(Name);
	this->X=X;
	this->Y=Y;
	this->Width=Width;
	this->Height=Height;
	this->FlatStyle=FlatStyle;
    this->ColorIndex=0;

	this->Text=wstrdup(Text);
	FontText=ResMgr->LoadFontR(L"宋体-14");
	ImgNormal=ResMgr->LoadScale9R(L"按钮Normal");
	ImgHover=ResMgr->LoadScale9R(L"按钮Hover");
	ImgPush=ResMgr->LoadScale9R(L"按钮Push");
	ImgDisable=ResMgr->LoadScale9R(L"按钮Disable");
}

CButton::~CButton()
{
	ResMgr->ReleaseFontR(FontText);
	ResMgr->ReleaseScale9R(ImgNormal);
	ResMgr->ReleaseScale9R(ImgHover);
	ResMgr->ReleaseScale9R(ImgPush);
	ResMgr->ReleaseScale9R(ImgDisable);
	delete[] Text;

	delete[] Name;
}

void CButton::Draw()
{
	if (!FlatStyle)
	{
		ImageR_t* BkImg=ImgNormal;
		if (!Enable)
			BkImg=ImgDisable;
		else if (Hovered)
			BkImg=ImgHover;
		else if (Pushed)
			BkImg=ImgPush;
		D2dMgr->SetImageOpacity(1.0f);
		D2dMgr->DrawImage(0,0,Width,Height,BkImg);
	}
	else
	{
		DWORD NormalColor;
		DWORD HoverColor;
		DWORD PushColor;
		if (ColorIndex==0)
		{
			NormalColor=0xFFE5A766;
			HoverColor=0xFFE9C299;
			PushColor=0xFFC07000;
		}
		else if (ColorIndex==1)
		{
			NormalColor=0xFFE41C62;
			HoverColor=0xFFFD0370;
			PushColor=0xFFCA1D59;
		}
		else if (ColorIndex==2)
		{
			NormalColor=0xFF5BE2DA;
			HoverColor=0xFF4AF3E9;
			PushColor=0xFF55C9c0;
		}
		else if (ColorIndex==3)
		{
			NormalColor=0xFF48E056;
			HoverColor=0xFF38FF4B;
			PushColor=0xFF40B34B;
		}
		else if (ColorIndex==4)
		{
			NormalColor=0xFF892058;
			HoverColor=0xFFAA3272;
			PushColor=0xFF6B084B;
		}
		DWORD BackColor=NormalColor;
		if (!Enable)
			BackColor=0xFFA6A6A6;
		else if (Hovered)
			BackColor=HoverColor;
		else if (Pushed)
			BackColor=PushColor;
		D2dMgr->SetColor(BackColor);
		D2dMgr->FillRectangle(0,0,Width,Height);
		D2dMgr->SetColor(0xFF404040);
		D2dMgr->DrawRectangle(0,0,Width,Height);
	}

	DWORD TextColor=0xFFFFFF00;
	DWORD StrokeColor=0x80000000;
	if (!Enable)
	{
		TextColor=0xFFC0C0C0;
		StrokeColor=0x80000000;
	}

	int TextWidth=D2dMgr->GetTextWidth(FontText,Text);
	int TextHeight=D2dMgr->GetFontHeight(FontText);
	int TextX=Width/2-TextWidth/2;
	int TextY=Height/2-TextHeight/2;
	D2dMgr->SetColor(TextColor);
	D2dMgr->DrawTextWithBorder(Text,-1,TextX,TextY,StrokeColor,FontText);
}

int CButton::Notify(UIEvent_t UIEvent,PVOID Param1,PVOID Param2)
{
	if (!Enable)
		return 0;
	if (UIEvent==EvtClick && EventHandler!=NULL)
	{
		EventHandler(this,EvtClick,(PVOID)0,(PVOID)0);
	}
	return 0;
}

void CButton::SetText(WCHAR* Text)
{
	delete[] this->Text;
	this->Text=wstrdup(Text);
}

WCHAR* CButton::GetText()
{
	return Text;
}

void CButton::SetFlatColor(int Index)
{
	if (Index>=0 && Index<=4)
		this->ColorIndex=Index;
}


//CImage
//////////////////////////////////////////////////////////////////////////
CImage::CImage(WCHAR* Name,int X,int Y,int Width,int Height)
{
	Type=CT_Image;
	this->Name=wstrdup(Name);
	this->X=X;
	this->Y=Y;
	this->Width=Width;
	this->Height=Height;

	ImgMain=NULL;
	ImgHover=NULL;
	ImgPush=NULL;
	ImgDisable=NULL;
	ImageScale=TRUE;
}

CImage::~CImage()
{
	if (ImgMain!=NULL)
		ResMgr->ReleaseImageNoType(ImgMain);
	if (ImgHover!=NULL)
		ResMgr->ReleaseImageNoType(ImgHover);
	if (ImgPush!=NULL)
		ResMgr->ReleaseImageNoType(ImgPush);
	if (ImgDisable!=NULL)
		ResMgr->ReleaseImageNoType(ImgDisable);

	delete[] Name;
}

void CImage::Draw()
{
	ImageR_t* TmpImgHover=ImgHover;
	if (ImgHover==NULL)
		TmpImgHover=ImgMain;
	ImageR_t* TmpImgPush=ImgPush;
	if (TmpImgPush==NULL)
	{
		if (ImgHover==NULL)
			TmpImgPush=ImgMain;
		else
			TmpImgPush=ImgHover;
	}
	ImageR_t* TmpImgDisable=ImgDisable;
	if (TmpImgDisable==NULL)
		TmpImgDisable=ImgMain;

	ImageR_t* ImgDraw=ImgMain;
	if (!Enable)
		ImgDraw=TmpImgDisable;
	else if (Hovered)
		ImgDraw=TmpImgHover;
	else if (Pushed)
		ImgDraw=TmpImgPush;

	D2dMgr->SetImageOpacity(1.0f);
	if (ImgDraw!=NULL)
	{
		if (ImageScale)
		{
			D2dMgr->DrawImage(0,0,Width,Height,ImgDraw);
		}
		else
		{
			int ImgWidth=ResMgr->GetImageWidth(ImgDraw);
			int ImgHeight=ResMgr->GetImageHeight(ImgDraw);
			D2dMgr->DrawImage(Width/2-ImgWidth/2,Height/2-ImgHeight/2,ImgWidth,ImgHeight,ImgDraw);
			D2dMgr->SetColor(0x40000000);
			D2dMgr->DrawRectangle(0,0,Width,Height);
		}
	}

#ifdef _DEBUG
	D2dMgr->SetColor(0xFF000000);
	D2dMgr->DrawRectangle(0,0,Width,Height);
#endif
}

int CImage::Notify(UIEvent_t UIEvent,PVOID Param1,PVOID Param2)
{
	if (!Enable)
		return 0;
	if (UIEvent==EvtClick && EventHandler!=NULL)
	{
		EventHandler(this,EvtClick,(PVOID)0,(PVOID)0);
	}
	return 0;
}

void CImage::SetImage(ImageR_t* ImgMain)
{
	if (this->ImgMain!=NULL)
		ResMgr->ReleaseImageNoType(this->ImgMain);
	this->ImgMain=ImgMain;
}

void CImage::SetHoverImage(ImageR_t* ImgHover)
{
	if (this->ImgHover!=NULL)
		ResMgr->ReleaseImageNoType(this->ImgHover);
	this->ImgHover=ImgHover;
}

void CImage::SetPushImage(ImageR_t* ImgPush)
{
	if (this->ImgPush!=NULL)
		ResMgr->ReleaseImageNoType(this->ImgPush);
	this->ImgPush=ImgPush;
}

void CImage::SetDisableImage(ImageR_t* ImgDisable)
{
	if (this->ImgDisable!=NULL)
		ResMgr->ReleaseImageNoType(this->ImgDisable);
	this->ImgDisable=ImgDisable;
}

void CImage::EnableImageScale(BOOL Enable)
{
	this->ImageScale=Enable;
}


//CLabel
//////////////////////////////////////////////////////////////////////////
CLabel::CLabel(WCHAR* Name,int X,int Y,int Width,int Height,WCHAR* Text)
{
	Type=CT_Label;
	this->Name=wstrdup(Name);
	this->X=X;
	this->Y=Y;
	this->Width=Width;
	this->Height=Height;

	this->Text=wstrdup(Text);
	Color=0xFF000000;
	BackColor=0x00000000;
	BorderColor=0x00000000;
	Padding=0;
	LineSpace=0;
	FontText=ResMgr->LoadFontR(L"宋体-14");
}

CLabel::~CLabel()
{
	ResMgr->ReleaseFontR(FontText);
	delete[] Text;
	delete[] Name;
}

void CLabel::Draw()
{
	int Len=wcslen(Text);
	if (BackColor!=0x00000000)
	{
		D2dMgr->SetColor(BackColor);
		D2dMgr->FillRectangle(0,0,Width,Height);
	}

	D2dMgr->SetColor(Color);
	D2dMgr->AutoDrawText(Text,-1,Padding,Padding,Width-Padding*2,Height-Padding*2,0,LineSpace,FontText);

	if (BorderColor!=0x00000000)
	{
		D2dMgr->SetColor(BorderColor);
		D2dMgr->DrawRectangle(0,0,Width,Height);
	}
}

int CLabel::Notify(UIEvent_t UIEvent,PVOID Param1,PVOID Param2)
{
	if (!Enable)
		return 0;
	if (UIEvent==EvtClick && EventHandler!=NULL)
	{
		EventHandler(this,EvtClick,(PVOID)0,(PVOID)0);
	}
	return 0;
}

void CLabel::SetText(WCHAR* Text)
{
	delete[] this->Text;
	this->Text=wstrdup(Text);
}

WCHAR* CLabel::GetText()
{
	return Text;
}

void CLabel::SetTextColor(DWORD ColorArgb)
{
	this->Color=ColorArgb;
}

void CLabel::SetBackColor(DWORD ColorArgb)
{
	this->BackColor=ColorArgb;
}

void CLabel::SetBorderColor(DWORD ColorArgb)
{
	this->BorderColor=ColorArgb;
}

void CLabel::SetPadding(int Padding)
{
	this->Padding=Padding;
}

void CLabel::SetLineSpace(int LineSpace)
{
	this->LineSpace=LineSpace;
}


//CLineLabel
//////////////////////////////////////////////////////////////////////////
CLineLabel::CLineLabel(WCHAR* Name,int X,int Y,int Width,int Height,WCHAR* Text)
{
	Type=CT_Label;
	this->Name=wstrdup(Name);
	this->X=X;
	this->Y=Y;
	this->Width=Width;
	this->Height=Height;

	this->Text=wstrdup(Text);
	Color=0xFF000000;
	BackColor=0x00000000;
	BorderColor=0x00000000;
	TextBorderColor=0x00000000;
	AlignSymbol='<';
	FontText=ResMgr->LoadFontR(L"宋体-14");
}

CLineLabel::~CLineLabel()
{
	ResMgr->ReleaseFontR(FontText);
	delete[] Text;
	delete[] Name;
}

void CLineLabel::Draw()
{
	int Len=wcslen(Text);
	if (BackColor!=0x00000000)
	{
		D2dMgr->SetColor(BackColor);
		D2dMgr->FillRectangle(0,0,Width,Height);
	}

	int TextHeight=D2dMgr->GetFontHeight(FontText);
	int DrawY=Height/2-TextHeight/2;

	int TextWidth=D2dMgr->GetTextWidth(FontText,Text);
	int DrawX=0;
	if (AlignSymbol=='>')
		DrawX=Width-TextWidth;
	else if (AlignSymbol=='-')
		DrawX=Width/2-TextWidth/2;

	if (TextBorderColor!=0)
	{
		if (AlignSymbol=='>')
			DrawX--;
		else
			DrawX++;
		D2dMgr->PushClip(DrawX-1,DrawY-1,TextWidth+2,TextHeight+2);
	}
	else
	{
		D2dMgr->PushClip(DrawX,DrawY,TextWidth,TextHeight);
	}
	D2dMgr->SetColor(Color);
	D2dMgr->DrawTextWithBorder(Text,-1,DrawX,DrawY,TextBorderColor,FontText);
	D2dMgr->PopClip();

	if (BorderColor!=0x00000000)
	{
		D2dMgr->SetColor(BorderColor);
		D2dMgr->DrawRectangle(0,0,Width,Height);
	}
}

int CLineLabel::Notify(UIEvent_t UIEvent,PVOID Param1,PVOID Param2)
{
	if (!Enable)
		return 0;
	if (UIEvent==EvtClick && EventHandler!=NULL)
	{
		EventHandler(this,EvtClick,(PVOID)0,(PVOID)0);
	}
	return 0;
}

void CLineLabel::SetText(WCHAR* Text)
{
	delete[] this->Text;
	this->Text=wstrdup(Text);
}

WCHAR* CLineLabel::GetText()
{
	return Text;
}

void CLineLabel::SetTextColor(DWORD ColorArgb)
{
	this->Color=ColorArgb;
}

void CLineLabel::SetBackColor(DWORD ColorArgb)
{
	this->BackColor=ColorArgb;
}

void CLineLabel::SetBorderColor(DWORD ColorArgb)
{
	this->BorderColor=ColorArgb;
}

void CLineLabel::SetTextBorderColor(DWORD ColorArgb)
{
	this->TextBorderColor=ColorArgb;
}

void CLineLabel::SetAlign(int AlignSymbol)
{
	if (AlignSymbol!='<' && AlignSymbol!='-' && AlignSymbol!='>')
		return ;
	this->AlignSymbol=AlignSymbol;
}


//CCheckBox
//////////////////////////////////////////////////////////////////////////
CCheckBox::CCheckBox(WCHAR* Name,int X,int Y,WCHAR* Text)
{
	Type=CT_CheckBox;
	this->Name=wstrdup(Name);
	this->X=X;
	this->Y=Y;

	this->Text=wstrdup(Text);
	FontText=ResMgr->LoadFontR(L"宋体-14");
	ImgNormal[0]=ResMgr->LoadImageR(L"未选中Normal");
	ImgNormal[1]=ResMgr->LoadImageR(L"选中Normal");
	ImgHover[0]=ResMgr->LoadImageR(L"未选中Hover");
	ImgHover[1]=ResMgr->LoadImageR(L"选中Hover");
	ImgPush[0]=ResMgr->LoadImageR(L"未选中Push");
	ImgPush[1]=ResMgr->LoadImageR(L"选中Push");
	ImgDisable[0]=ResMgr->LoadImageR(L"未选中Disable");
	ImgDisable[1]=ResMgr->LoadImageR(L"选中Disable");

	TextColor=0xFFEAC668;
	BackColor=0x00000000;

	int TextWidth=D2dMgr->GetTextWidth(FontText,this->Text);
	this->Width=CheckWidth+Padding*2+TextWidth+Padding;

	this->Height=CheckHeight+Padding*2;

	Checked=FALSE;
}

CCheckBox::~CCheckBox()
{
	ResMgr->ReleaseImageR(ImgNormal[0]);
	ResMgr->ReleaseImageR(ImgNormal[1]);
	ResMgr->ReleaseImageR(ImgHover[0]);
	ResMgr->ReleaseImageR(ImgHover[1]);
	ResMgr->ReleaseImageR(ImgPush[0]);
	ResMgr->ReleaseImageR(ImgPush[1]);
	ResMgr->ReleaseImageR(ImgDisable[0]);
	ResMgr->ReleaseImageR(ImgDisable[1]);

	ResMgr->ReleaseFontR(FontText);
	delete[] Text;
	delete[] Name;
}

void CCheckBox::Draw()
{
	if (BackColor!=0x00000000)
	{
		D2dMgr->SetColor(BackColor);
		D2dMgr->FillRectangle(0,0,Width,Height);
	}

	ImageR_t* CheckImg=ImgNormal[Checked];
	if (!Enable)
		CheckImg=ImgDisable[Checked];
	else if (Hovered)
		CheckImg=ImgHover[Checked];
	else if (Pushed)
		CheckImg=ImgPush[Checked];

	int CheckPosX=Padding;
	int CheckPosY=Height/2-CheckHeight/2;
	D2dMgr->SetImageOpacity(1.0f);
	D2dMgr->DrawImage(CheckPosX,CheckPosY,CheckWidth,CheckHeight,CheckImg);

	int TextPosX=CheckWidth+Padding*2;
	int TextPosY=Height/2-D2dMgr->GetFontHeight(FontText)/2;
	if (TextColor!=0x00000000)
		D2dMgr->SetColor(TextColor);
	D2dMgr->DrawText(Text,-1,TextPosX,TextPosY,FontText);
	if (!Enable)
	{
		D2dMgr->SetColor(0x40000000);
		D2dMgr->DrawText(Text,-1,TextPosX,TextPosY,FontText);
	}

#ifdef _DEBUG
	D2dMgr->SetColor(0xFF000000);
	D2dMgr->DrawRectangle(0,0,Width,Height);
#endif
}

int CCheckBox::Notify(UIEvent_t UIEvent,PVOID Param1,PVOID Param2)
{
	if (!Enable)
		return 0;
	if (UIEvent==EvtClick)
	{
		Checked=!Checked;
		if (EventHandler!=NULL)
			EventHandler(this,EvtCheckBoxChange,(PVOID)Checked,(PVOID)0);
	}
	return 0;
}

void CCheckBox::SetText(WCHAR* Text)
{
	delete[] this->Text;
	this->Text=wstrdup(Text);

	int TextWidth=D2dMgr->GetTextWidth(FontText,this->Text);
	this->Width=CheckWidth+Padding*2+TextWidth+Padding;
	int TextHeight=D2dMgr->GetFontHeight(FontText);
	this->Height=max(CheckHeight,TextHeight)+Padding*2;
}

WCHAR* CCheckBox::GetText()
{
	return Text;
}

void CCheckBox::SetCheck(BOOL Checked)
{
	if (Checked!=0)
		Checked=1;
	if (this->Checked!=Checked)
	{
		this->Checked=Checked;
		if (EventHandler!=NULL)
			EventHandler(this,EvtCheckBoxChange,(PVOID)Checked,(PVOID)0);
	}
}

BOOL CCheckBox::IsChecked()
{
	return Checked;
}

void CCheckBox::SetTextColor(DWORD ColorArgb)
{
	this->TextColor=ColorArgb;
}

void CCheckBox::SetBackColor(DWORD ColorArgb)
{
	this->BackColor=ColorArgb;
}